var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PREVIOUS_PLAY_DATE_STORAGE_KEY = exports.LAST_PALY_DATE_STORAGE_KEY = exports.PREVIOUS_PLAY_TIME_STORAGE_KEY = exports.LAST_PALY_TIME_STORAGE_KEY = undefined;
var $1$zqcc_OverallSystem = require("zqcc_OverallSystem");
var i = cc__importDefault($1$zqcc_OverallSystem);
exports.LAST_PALY_TIME_STORAGE_KEY = "Last-Play-Time";
exports.PREVIOUS_PLAY_TIME_STORAGE_KEY = "Previous-Play-Time";
exports.LAST_PALY_DATE_STORAGE_KEY = "Last-Play-Date";
exports.PREVIOUS_PLAY_DATE_STORAGE_KEY = "Previous-Play-Date";
var r = function () {
  function t() {
    // 初始化日期对象和播放时间相关的变量
    this.dateObj = new Date();
    this.lastPlayTime = undefined;
    this.previousPlayTime = undefined;
    this.lastPlayDate = undefined;
    this.oldLastPlayDate = undefined; // 统一命名风格
    this.previousPlayDate = undefined;
    this.scheduler = undefined;
    this.oneSecondsCBMap = {};

    // 打印日志
    console.log("TimePlugIn");

    // 从本地存储获取最近播放时间
    let lastPlayTimeStored = i.default.Servers.localStorageSrv.get(exports.LAST_PALY_TIME_STORAGE_KEY);
    if (lastPlayTimeStored) { // 简化 null 和空字符串的检查
      this.previousPlayTime = JSON.parse(lastPlayTimeStored);
      i.default.Servers.localStorageSrv.set(exports.PREVIOUS_PLAY_TIME_STORAGE_KEY, JSON.stringify(this.previousPlayTime));
      console.log("this.lastPlayTime :", this.timeFabricToString(this.previousPlayTime));
    } else {
      this.lastPlayTime = this.getNowTime();
      i.default.Servers.localStorageSrv.set(exports.LAST_PALY_TIME_STORAGE_KEY, JSON.stringify(this.lastPlayTime));
    }

    // 从本地存储获取最近播放日期
    let lastPlayDateStored = i.default.Servers.localStorageSrv.get(exports.LAST_PALY_DATE_STORAGE_KEY);
    if (lastPlayDateStored) {
      this.previousPlayDate = JSON.parse(lastPlayDateStored);
      this.oldLastPlayDate = this.previousPlayDate;
      this.lastPlayDate = Date.now();
    } else {
      this.previousPlayDate = this.lastPlayDate = this.oldLastPlayDate = Date.now();
    }

    // 更新本地存储中的播放日期
    i.default.Servers.localStorageSrv.set(exports.LAST_PALY_DATE_STORAGE_KEY, JSON.stringify(this.lastPlayDate));
    i.default.Servers.localStorageSrv.set(exports.PREVIOUS_PLAY_DATE_STORAGE_KEY, JSON.stringify(this.previousPlayDate));

    // 设置调度器
    this.scheduler = cc.director.getScheduler();
    this.scheduler.enableForTarget(this);
    this.scheduler.scheduleUpdate(this, cc.Scheduler.PRIORITY_SYSTEM, false);
    this.scheduler.schedule(this.updateOneSeconds, this, 1);
  }
  t.prototype.getPreviousPlayTime = function () {
    // 返回上次播放时间，如果不存在则返回 null
    return this.previousPlayTime || null;
  };
  t.prototype.getPreviousPlayDate = function () {
    return this.previousPlayDate;
  };
  t.prototype.getPreviousPlayDate2 = function () {
    return this.OldlastPlayDate;
  };
  t.prototype.getNowTime = function () {
    return {
      year: this.dateObj.getFullYear(),
      month: this.dateObj.getMonth() + 1,
      date: this.dateObj.getDate(),
      hours: this.dateObj.getHours(),
      minutes: this.dateObj.getMinutes(),
      seconds: this.dateObj.getSeconds(),
      millisecondes: this.dateObj.getMilliseconds(),
      day: this.dateObj.getDay()
    };
  };
  t.prototype.timeFabricToString = function (t) {
    return t.year + "年/" + t.month + "月/" + t.date + "日/" + t.hours + "时/" + t.minutes + "分/" + t.seconds + "秒/" + t.millisecondes + "毫秒/星期" + t.day;
  };
  t.prototype.updateLastPlayTime = function () {
    // 获取当前时间并更新最近播放时间和日期
    this.lastPlayTime = this.getNowTime();
    i.default.Servers.localStorageSrv.set(exports.LAST_PALY_TIME_STORAGE_KEY, JSON.stringify(this.lastPlayTime));

    this.lastPlayDate = Date.now();
    i.default.Servers.localStorageSrv.set(exports.LAST_PALY_DATE_STORAGE_KEY, JSON.stringify(this.lastPlayDate));
  };
  t.prototype.setScheduleOneSeconds = function (t, e) {
    this.oneSecondsCBMap[t] = e;
  };
  t.prototype.deleteScheduleOneSeconds = function (t) {
    delete this.oneSecondsCBMap[t];
  };
  t.prototype.updateOneSeconds = function () {
    // 更新最近播放时间
    this.updateLastPlayTime();

    // 遍历并执行每个回调函数
    for (const key in this.oneSecondsCBMap) {
      const callback = this.oneSecondsCBMap[key];
      if (callback) {
        callback(); // 调用有效的回调函数
      }
    }
  };
  t.prototype.update = function () {
    this.dateObj = new Date();
  };
  t.prototype.numberShiftTime = function (t, e) {
    // 确保 e 有默认值为 false
    if (e === undefined) {
      e = false;
    }

    let output = "";
    const seconds = t % 60; // 秒数
    let minutes = Math.floor(t / 60); // 计算分钟数
    let hours = 0;

    // 计算小时数
    if (minutes > 59) {
      hours = Math.floor(minutes / 60);
      minutes %= 60;
    }

    // 处理小时部分的输出
    if (hours <= 0) {
      if (e) {
        output = "00:";
      }
    } else {
      output = (hours > 9 ? "" + hours : "0" + hours) + ":";
    }

    // 处理分钟和秒的输出
    if (minutes > 0) {
      output += (e ? (minutes > 9 ? "" + minutes : "0" + minutes) + ":" : "") ||
        (minutes > 9 ? "" + minutes : "0" + minutes) + ":";
    } else {
      output += e ? "00:" : (output === "" ? "00:" : output + "00:");
    }

    // 处理秒的输出
    output += (seconds > 0 ? (seconds > 9 ? "" + seconds : "0" + seconds) : "00");

    return output;

  };
  t.compareTime = function (t, e, o) {
    for (const key in t) {
      if (t.hasOwnProperty(key)) {
        if (key === "day") {
          continue; // 跳过 "day" 属性
        }

        const valueT = t[key];
        const valueE = e[key];
        const difference = Math.max(valueT, valueE) - Math.min(valueT, valueE);

        if (!o[key] && difference > 0) {
          return true; // 存在差异，且 o[key] 不存在
        }

        if (o[key] && difference >= o[key]) {
          return true; // 差异大于或等于 o[key]
        }

        if (o[key] && difference < o[key]) {
          return false; // 差异小于 o[key]
        }
      }
    }
    return false; // 默认返回 false
  };
  return t;
}();
var a = null;
exports.default = function () {
  null == a && (a = new r());
  return a;
};